package com.example.huanhang.changzhouapp.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;

import com.example.huanhang.changzhouapp.R;
import com.example.huanhang.changzhouapp.base.BaseBackActivity;
import com.example.huanhang.changzhouapp.bean.User;
import com.gyf.barlibrary.ImmersionBar;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author lei
 * @desc 反馈页面
 * @date 2018/11/2 0002 -- 上午 10:19.
 */
public class FanKuiActivity extends BaseBackActivity {

    @BindView(R.id.webview)
    WebView mWebview;
    private String mUserId;
    private String mName;


    @Override
    public int getLayoutId() {
        return R.layout.activity_fan_kui;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        setTitle("意见反馈");
        ImmersionBar.with(this).statusBarColor(R.color.title).init();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        showWaitDialog("加载中...");
        mWebview.getSettings().setJavaScriptEnabled(true);
        mWebview.getSettings().setDomStorageEnabled(true);
    }

    @Override
    public void initData() {
          //调起微信
        WebViewClient webViewClient = new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                super.shouldOverrideUrlLoading(view, url);
                if (url == null) {
                    return false;
                }
                try {
                    if (url.startsWith("weixin://")) {
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        view.getContext().startActivity(intent);
                        return true;
                    }
                } catch (Exception e) {
                    return false;
                }
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String s) {
                super.onPageFinished(view, s);
                dismissWaitDialog();
            }
        };
        mWebview.setWebViewClient(webViewClient);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEventMainThread(User user) {
        mUserId = user.getId();
        mName = user.getGzName();
        String version = getVersion();
        // 用户的头像url
        String headimgurl = "https://tucao.qq.com/static/desktop/img/products/def-product-logo.png";
        String url = "https://support.qq.com/product/41948";
        /* 准备post参数 */
        String postData = "nickname=" + mName + "&avatar=" + headimgurl + "&openid=" + mUserId+"&clientVersion="+version;
        mWebview.postUrl(url, postData.getBytes());

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && mWebview.canGoBack()) {
            mWebview.goBack();//返回上个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);

    }
}
